//
//  ArmListViewController.m
//  wAudioShare
//
//  Created by Anson on 14-11-8.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "ArmListViewController.h"
#import "Utitlity.h"
#import "ArmModel.h"
#import "ArmListViewCell.h"
#import "ArmEditViewController.h"
#import "ArmItemInfoView.h"
#import "ArmItemInfoCreateView.h"
#import "ChangeInfoDialog.h"
#import "TimingSettingViewController.h"

@interface ArmListViewController () <UIActionSheetDelegate, UITextFieldDelegate, ArmListViewCellDelegate,UIPickerViewDelegate,UIPickerViewDataSource, UITableViewDataSource, UITableViewDelegate, ActionPickerDelegate, ArmItemInfoViewDelegate, changeDataDicDelegate>
{
    
    SceneDeviceType _currentSelectDeviceType;
    NSArray *_temperatureArray;
    
    NSArray *_titleDimmerLight;
    NSArray *_titleFuncionOnOff;
    NSArray *_titleSensor;
    NSArray *_titlePowerMeter;
    NSArray *_titleCondition;
    NSArray *_titleThermostat;
    
    UIPickerView *_pickerView_DeviceList;
    UIPickerView *_pickerView_FuncTitleList;
    UIPickerView *_pickerView_Condition;
    
    UIPickerView *ios8Picker;
    
    NSArray *icons;
}


@property (strong, nonatomic) NSArray *deviceList;//用于编辑Scene时可选择添加设备列表

@property (weak, nonatomic) IBOutlet UIView *armNewInputView;
@property (weak, nonatomic) IBOutlet UITextField *tfArmNewTitle;
@property (weak, nonatomic) IBOutlet UIButton *btnDeviceListSelect;
@property (weak, nonatomic) IBOutlet UIButton *btnFuncListSelect;
@property (weak, nonatomic) IBOutlet UIButton *btnArmConditionSelect;
@property (weak, nonatomic) IBOutlet UITextField *tfArmConditionVal;
@property (weak, nonatomic) IBOutlet UILabel *labelCondition;

@property (nonatomic,retain) ActionPicker *actionPicker;

@property (strong, nonatomic) NSDictionary *selectedDeviceItemData;//准备要添加已选设备
//@property (strong, nonatomic) NSArray *funcTitleList;

@property (strong, nonatomic) ArmModel *aNewArmCondData;//用于新增的条件

@end

@implementation ArmListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
  
    icons = [NSArray arrayWithObjects:@"scene_0",@"scene_1",@"scene_2",@"scene_3", nil];
    
    closeNextPage = YES;
    threadLoadData = NO;
    
    BottomView *bottomView = [self addBottomView];
    bottomView.btn_camera.hidden = NO;
    //[bottomView.btn_camera addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];
    
    bottomView.btn_all_power.hidden = NO;
    [bottomView.btn_all_power addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
    
    dataTableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - bottomView.frame.size.height);
    
    
    //dataTableView.frame = CGRectMake(0, 0, 320, self.view.frame.size.height - 60);
    self.title =@"条件";
    
    [self getConditionList];

    [self getDeviceList];
    
    [self initAll];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initAll {
    
    _titleFuncionOnOff = @[@"ON", @"OFF"];
    _titleSensor = @[@"触发", @"正常"];
    _titlePowerMeter = @[@"ON", @"OFF", @"电流", @"电压"];
    _titleDimmerLight = @[@"亮度值"];
    _titleCondition = @[@"=",@">",@"<"];
    _titleThermostat = @[@"温度"];
    
    
    _pickerView_DeviceList = [[UIPickerView alloc]initWithFrame:CGRectZero];
    _pickerView_DeviceList.dataSource = self;
    _pickerView_DeviceList.delegate = self;
    _pickerView_DeviceList.tag = PICKER_VIEW_DEVICE_LIST_TAG;
    _pickerView_DeviceList.showsSelectionIndicator = YES;
    
    _pickerView_FuncTitleList = [[UIPickerView alloc]initWithFrame:CGRectZero];
    _pickerView_FuncTitleList.dataSource = self;
    _pickerView_FuncTitleList.delegate = self;
    _pickerView_FuncTitleList.tag = PICKER_VIEW_FUNCTION_LIST_TAG;
    _pickerView_FuncTitleList.showsSelectionIndicator = YES;
    
    
    _pickerView_Condition = [[UIPickerView alloc] initWithFrame:CGRectZero];
    _pickerView_Condition.dataSource = self;
    _pickerView_Condition.delegate = self;
    _pickerView_Condition.tag = PICKER_VIEW_TAG_CONDITION;
    _pickerView_Condition.showsSelectionIndicator = YES;

}

- (void)reSetDevListSelect {
    
    _currentSelectDeviceType = SceneDeviceTypeInvalid;
    self.aNewArmCondData = nil;
    self.selectedDeviceItemData = nil;
    self.tfArmConditionVal.text = @"";
    self.tfArmNewTitle.text = @"";
    [self.btnFuncListSelect setTitle:@"" forState:UIControlStateNormal];
    [self.btnArmConditionSelect setTitle:@"" forState:UIControlStateNormal];
    [self.btnDeviceListSelect setTitle:@"" forState:UIControlStateNormal];
    
    [_pickerView_DeviceList reloadAllComponents];
}

#pragma mark - Http Request
- (void)getConditionList {

    ipUrl = [Utitlity judgeipUrl];
    if (ipUrl) {
    
    NSString *loadStr = [NSString stringWithFormat:@"http://%@/cgi-bin/Cond.cgi?action=do_list", ipUrl];
    [Utitlity commonHTTPMethod:loadStr responseType:jsonType success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (!responseObject || ![responseObject isKindOfClass:[NSDictionary class]]) return;
        
        self.rows = [NSMutableArray arrayWithCapacity:10];
        NSArray *devList = responseObject[@"data"];
        for (NSDictionary *item in devList) {
            
            ArmModel *data = [[ArmModel alloc] initWithJSON:item];
            [self.rows addObject:data];
        }
        NSLog(@"resultItem:%@",responseObject);
        [dataTableView reloadData];
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    }
}

- (void)getDeviceList {
    
    ipUrl = [Utitlity judgeipUrl];
    if (ipUrl) {

    NSString *loadStr = [NSString stringWithFormat:@"http://%@/cgi-bin/Get.cgi?Get=1", ipUrl];
    [Utitlity commonHTTPMethod:loadStr responseType:jsonType success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (!responseObject || ![responseObject isKindOfClass:[NSDictionary class]]) return;
        
        NSMutableArray *resultItem = [[NSMutableArray alloc] init];
        NSArray *devList = responseObject[@"data"];
        for (NSDictionary *item in devList) {
            
            //NSInteger deviceType = [[item objectForKey:@"DeviceType"] integerValue];
            {
                [resultItem addObject:item];
            }
        }
        
        self.deviceList = resultItem;
        NSLog(@"resultItem:%@",responseObject);
        
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    }
}

- (void)requestForNewArm:(ArmModel *)armData {
    
    switch (_currentSelectDeviceType) {
        case SceneDeviceTypeSwitch:
            armData.command = @"37";
            armData.index = 0;
            break;
//        case SceneDeviceTypePowerMeter:
//            armData.command = @"37";
//            armData.index = 0;
//            break;
     
        case SceneDeviceTypeBinarySensor:
            armData.command = @"48";
            armData.index = 0;
            break;
        
        case SceneDeviceTypeMultilevelSensor:
            armData.command = @"49";
            armData.index = 0;
            break;
            
        case SceneDeviceTypeDimmer:
            armData.command = @"38";
            armData.index = 0;
            break;
        case SceneDeviceTypeThermostat:
            armData.command = @"49";
            armData.index = 1;
            break;
        case SceneDeviceTypeMotor:
            armData.command = @"38";
            armData.index = 0;
            break;
        case SceneDeviceTypeDoorLock:
            armData.command = @"0x62";
            armData.index = 0;
        default:
            break;
    }
    
    ipUrl = [Utitlity judgeipUrl];
    if (ipUrl) {
        
        [activity startAnimating];
        
    ASIFormDataRequest *formDataRequest = [ASIFormDataRequest requestWithURL:nil];
    NSString *encodedArmName = [formDataRequest encodeURL:armData.label];
    NSString *encodedConditionVal = [formDataRequest encodeURL:armData.condition];
    
    NSString *loadStr = [NSString stringWithFormat:@"http://%@/cgi-bin/Cond.cgi?action=do_create&label=%@&did=%@&cmd=%@&index=%ld&value=%@&cond=%@", ipUrl, encodedArmName, armData.did, armData.command, armData.index, armData.value, encodedConditionVal];
    
    [Utitlity commonHTTPMethod:loadStr responseType:jsonType
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
            [activity stopAnimating];
        if (!responseObject) return;
        NSDictionary *retJsonData = responseObject;
        if ( [[[retJsonData objectForKey:@"state"] lowercaseString] isEqualToString:@"success"] ) {
            
            ArmModel *newArmData = [[ArmModel alloc] initWithJSON:[retJsonData objectForKey:@"data"]];
            [self.rows addObject:newArmData];
            [dataTableView reloadData];
        }
           
        
     }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"error:%@, httpStatusCode:%ld",error, operation.response.statusCode);
         [activity stopAnimating];
     }];
    }
}

- (void)deleteArm:(ArmModel *)armData {
    ipUrl = [Utitlity judgeipUrl];
    if (ipUrl) {

    [activity startAnimating];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://%@/cgi-bin/Cond.cgi?action=do_destroy&cond=%ld", ipUrl, armData.armId];
    
    [Utitlity commonHTTPMethod:urlStr responseType:jsonType success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [activity stopAnimating];
        
        if (!responseObject) return;
        NSDictionary *retJsonData = responseObject;
        if ( [[[retJsonData objectForKey:@"state"] lowercaseString] isEqualToString:@"success"] ) {
            
            [self.rows removeObject:armData];
            [dataTableView reloadData];
        }
        [dataTableView setEditing:NO];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [activity stopAnimating];
        NSLog(@"error:%@",error);
        [dataTableView setEditing:!dataTableView.editing animated:YES];
    }];
    }
}

//!修改条件名称
- (void)renameArm:(ArmModel *)armData withLabelTitle:(NSString *)newLabelTitle{
    ipUrl = [Utitlity judgeipUrl];
    if (ipUrl) {

    [activity startAnimating];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://%@/cgi-bin/Cond.cgi?action=do_setlabel&cond=%ld&label=%@", ipUrl, armData.armId, newLabelTitle];
    
    [Utitlity commonHTTPMethod:urlStr responseType:jsonType success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [activity stopAnimating];
        
        if (!responseObject) return;
        NSDictionary *retJsonData = responseObject;
        if ( [[[retJsonData objectForKey:@"state"] lowercaseString] isEqualToString:@"success"] ) {
       
            armData.label = newLabelTitle;
            
            [dataTableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [activity stopAnimating];
        NSLog(@"error:%@",error);
        [dataTableView setEditing:!dataTableView.editing animated:YES];
    }];
    }
}

//!Active 条件
- (void)requestArm:(ArmModel *)armData toActive:(BOOL)isActive withSwitchCtl:(UISwitch *)sw {
    ipUrl = [Utitlity judgeipUrl];
    if (ipUrl) {

    [activity startAnimating];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://%@/cgi-bin/Cond.cgi?action=do_setflag&cond=%ld&active=%@", ipUrl, armData.armId, (isActive?@"True":@"False")];
    
    [Utitlity commonHTTPMethod:urlStr responseType:jsonType success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [activity stopAnimating];
        
        if (!responseObject) return;
        NSDictionary *retJsonData = responseObject;
        
        if (armData.armId == 255) {
        //批量打开或关闭
            if ( [[[retJsonData objectForKey:@"state"] lowercaseString] isEqualToString:@"success"] ) {
                
                for (ArmModel *data in self.rows) {
                    data.active = isActive;
                }
                [dataTableView reloadData];
            }
            
        }else {
            
            if ( [[[retJsonData objectForKey:@"state"] lowercaseString] isEqualToString:@"success"] ) {
                armData.active = isActive;
                [sw setOn:isActive animated:YES];
                //[dataTableView reloadData];
            }else {
                [sw setOn:!isActive animated:YES];
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [activity stopAnimating];
        NSLog(@"error:%@",error);
        [dataTableView setEditing:!dataTableView.editing animated:YES];
    }];
    }
}

#pragma mark - Button

- (void)showMenu:(UIButton *)sender {
    
    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"添加条件" otherButtonTitles:@"删除条件", @"全部打开", @"全部关闭", nil];
    [menu showFromRect:sender.frame inView:self.view animated:YES];
}

- (IBAction)newArmConfirmBtnClicked:(id)sender {

    if (self.tfArmNewTitle.text.length == 0 || self.tfArmConditionVal.text.length == 0) {
        UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请把信息填写完整！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [prompt show];
        return;
    }
    self.aNewArmCondData.label = self.tfArmNewTitle.text;
    self.aNewArmCondData.value = self.tfArmConditionVal.text;
    
    [self requestForNewArm:self.aNewArmCondData];
    [self closeArmConditionView:sender];
}

- (IBAction)closeArmConditionView:(id)sender {
    [self.view endEditing:YES];
    [((ArmItemInfoCreateView *)[sender superview]) dismiss:sender];
}

- (IBAction)functionSelectBtnClicked:(id)sender {
    [self doChoose:PICKER_VIEW_FUNCTION_LIST_TAG];
}

- (IBAction)deviceSelectBtnClicked:(id)sender {
    [self doChoose:PICKER_VIEW_DEVICE_LIST_TAG];
}

- (IBAction)conditionSelectBtnClicked:(id)sender {
    [self doChoose:PICKER_VIEW_TAG_CONDITION];
}

- (IBAction)dimissKeyboard:(id)sender {
    
    [self.view endEditing:YES];
}

#pragma mark - UIActionSheet
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
        {
            ArmItemInfoCreateView  *view = [[[NSBundle mainBundle] loadNibNamed:@"ArmItemInfoCreateView" owner:self options:nil]lastObject];
            //view.delegate = self;
            [view initView];

            self.tfArmNewTitle = view.tfArmNewTitle;
            self.tfArmConditionVal = view.tfArmConditionVal;
            self.btnDeviceListSelect = view.btnDeviceListSelect;
            self.btnFuncListSelect = view.btnFuncListSelect;
            self.btnArmConditionSelect = view.btnArmConditionSelect;
            
            [view.btnDeviceListSelect addTarget:self action:@selector(deviceSelectBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [view.btnFuncListSelect addTarget:self action:@selector(functionSelectBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [view.btnArmConditionSelect addTarget:self action:@selector(conditionSelectBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [view.btnConfirm addTarget:self action:@selector(newArmConfirmBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            [view pageStart];
    
            //[self.view bringSubviewToFront:_armNewInputView];
            break;
        }
        case 1:
            [dataTableView setEditing:!dataTableView.editing animated:YES];
            break;
            
        case 2:
        {//!全打开
            ArmModel *data = [[ArmModel alloc] init];
            data.armId = 255;
            [self requestArm:data toActive:YES withSwitchCtl:nil];
            break;
        }
            
        case 3:
        {//！全部关闭
            ArmModel *data = [[ArmModel alloc] init];
            data.armId = 255;
            [self requestArm:data toActive:NO withSwitchCtl:nil];
            break;
        }
        default:
            break;
    }
}

#pragma mark - PickerView
-(void)doChoose:(NSInteger)tag
{
    UIPickerView *picker = nil;
    int index = 0;
    if (tag == PICKER_VIEW_DEVICE_LIST_TAG)
    {
        picker = _pickerView_DeviceList;
    }else if (tag == PICKER_VIEW_FUNCTION_LIST_TAG) {
        
        picker = _pickerView_FuncTitleList;
    }else if (tag == PICKER_VIEW_TAG_CONDITION) {
        picker = _pickerView_Condition;
    }

    if (!picker) return;
    
    [picker selectRow:index inComponent:0 animated:NO];
    
    //ios 7 的自定义UIActionSheet
    if ([[UIDevice currentDevice].systemVersion floatValue] < 8)
    {
        self.actionPicker = [[ActionPicker alloc]initWithTitle:@"" delegate:self picker:picker];
        self.actionPicker.tag = tag;
        [self.actionPicker showInView:self.view.superview];
    }
    else
    {
        [self alertWithPicer:picker];
    }
}

- (void)pickerViewConfirmForDevList:(NSInteger)picker_selected_index {
    NSDictionary *devData = [self.deviceList objectAtIndex:picker_selected_index];
    NSString *title = devData[@"Alias"];
    

    self.aNewArmCondData = [[ArmModel alloc] init];
    self.aNewArmCondData.did = devData[@"ZID"];
    
    _currentSelectDeviceType = [devData[@"DeviceType"] integerValue];
    self.selectedDeviceItemData = devData;
    
    [self.btnDeviceListSelect setTitle:title forState:UIControlStateNormal];
  
    [_pickerView_FuncTitleList reloadAllComponents];
    [self.btnFuncListSelect setTitle:@"" forState:UIControlStateNormal];
    [self.btnArmConditionSelect setTitle:@"" forState:UIControlStateNormal];
    self.tfArmConditionVal.text = @"";
}

- (void)pickerViewConfirmForFunctionList:(NSInteger)picker_selected_index {
    
    self.labelCondition.text = @"";
    
    switch (_currentSelectDeviceType) {

        case SceneDeviceTypeSwitch:
        case SceneDeviceTypeBinarySensor:
        case SceneDeviceTypeMotor:
        case SceneDeviceTypeDoorLock:
        {
            self.aNewArmCondData.condition = @"=";
            if (picker_selected_index == 0) {
                self.aNewArmCondData.value = @"255";
            }else {
                self.aNewArmCondData.value = @"0";
            }
            if (_currentSelectDeviceType == SceneDeviceTypeBinarySensor)
                [self.btnFuncListSelect setTitle:[_titleSensor objectAtIndex:picker_selected_index] forState:UIControlStateNormal];
            else
                [self.btnFuncListSelect setTitle:[_titleFuncionOnOff objectAtIndex:picker_selected_index] forState:UIControlStateNormal];
                
            [self.btnArmConditionSelect setTitle:self.aNewArmCondData.condition forState:UIControlStateNormal];
            self.tfArmConditionVal.text = self.aNewArmCondData.value;
            return;
        }
        
        case SceneDeviceTypePowerMeter:
        {
            [self.btnFuncListSelect setTitle:[_titlePowerMeter objectAtIndex:picker_selected_index] forState:UIControlStateNormal];
            if (picker_selected_index == 0) {
          
                self.aNewArmCondData.condition = @"=";
                self.aNewArmCondData.value = @"255";
                self.aNewArmCondData.command = @"37";
                self.aNewArmCondData.index = 0;
                
            }else if (picker_selected_index == 1) {
                
                self.aNewArmCondData.condition = @"=";
                self.aNewArmCondData.value = @"255";
                self.aNewArmCondData.command = @"37";
                self.aNewArmCondData.index = 0;
                
            }else if (picker_selected_index == 2) {
             //!电流
                self.tfArmConditionVal.text = @"";
                self.labelCondition.text = @"A";
                self.aNewArmCondData.command = @"50";
                self.aNewArmCondData.index = 20;
                
            }else if (picker_selected_index == 3) {
             //!电压
                self.tfArmConditionVal.text = @"";
                self.labelCondition.text = @"V";
                self.aNewArmCondData.command = @"50";
                self.aNewArmCondData.index = 16;
                
            }
            break;
        }
            
        case SceneDeviceTypeDimmer:
        {
            [self.btnFuncListSelect setTitle:[_titleDimmerLight objectAtIndex:picker_selected_index] forState:UIControlStateNormal];
            self.tfArmConditionVal.text = @"";
            self.labelCondition.text = @"%";
            break;
        }
        
        case SceneDeviceTypeThermostat:
        {
            [self.btnFuncListSelect setTitle:[_titleThermostat objectAtIndex:picker_selected_index] forState:UIControlStateNormal];
            self.tfArmConditionVal.text = @"";
            self.labelCondition.text = @"℃";
            break;
        }
            
        default:
            break;
    }
    
    [_pickerView_Condition reloadAllComponents];
    [self.btnArmConditionSelect setTitle:@"" forState:UIControlStateNormal];
    
}

- (void)pickerViewConfirmForCondition:(NSInteger)picker_selected_index {
 
    self.aNewArmCondData.condition = _titleCondition[picker_selected_index];
    [self.btnArmConditionSelect setTitle:self.aNewArmCondData.condition forState:UIControlStateNormal];
}

-(void)didOKButtonPressed:(ActionPicker *)picker
{
    if (picker.tag == PICKER_VIEW_DEVICE_LIST_TAG)
    {
        NSInteger picker_selected_index= [_pickerView_DeviceList selectedRowInComponent:0];
        [self pickerViewConfirmForDevList:picker_selected_index];
    
    }else if (picker.tag == PICKER_VIEW_FUNCTION_LIST_TAG) {
 
        NSInteger picker_selected_index= [_pickerView_FuncTitleList selectedRowInComponent:0];
        [self pickerViewConfirmForFunctionList:picker_selected_index];
        
    }else if (picker.tag == PICKER_VIEW_TAG_CONDITION) {
        
        NSInteger picker_selected_index= [_pickerView_Condition selectedRowInComponent:0];
        
        [self pickerViewConfirmForCondition:picker_selected_index];
    }
 
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView1 numberOfRowsInComponent:(NSInteger)component;
{
    if (pickerView1.tag == PICKER_VIEW_DEVICE_LIST_TAG)
    {
        return self.deviceList.count;
    }else if (pickerView1.tag == PICKER_VIEW_FUNCTION_LIST_TAG) {
    
        switch (_currentSelectDeviceType) {
            case SceneDeviceTypeMultilevelSensor:
            case SceneDeviceTypeBinarySensor:
                return _titleSensor.count;
                break;
            case SceneDeviceTypeDimmer:
                return _titleDimmerLight.count;
            case SceneDeviceTypePowerMeter:
                return _titlePowerMeter.count;
            case SceneDeviceTypeThermostat:
                return _titleThermostat.count;
            case SceneDeviceTypeSwitch:
            case SceneDeviceTypeMotor:
            case SceneDeviceTypeDoorLock:
                return _titleFuncionOnOff.count;
            default:
                return 0;
        }
        
    }else {
        
        return _titleCondition.count;
    }
 }

- (NSString *)pickerView:(UIPickerView *)pickerView1 titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView1.tag == PICKER_VIEW_DEVICE_LIST_TAG)
    {
        NSDictionary *devData = [self.deviceList objectAtIndex:row];
        return [devData objectForKey:@"Alias"];
    }else if (pickerView1.tag == PICKER_VIEW_FUNCTION_LIST_TAG) {
    
        switch (_currentSelectDeviceType) {
            case SceneDeviceTypeMultilevelSensor:
            case SceneDeviceTypeBinarySensor:
                return _titleSensor[row];
                break;
            case SceneDeviceTypeDimmer:
                return _titleDimmerLight[row];
            case SceneDeviceTypePowerMeter:
                return _titlePowerMeter[row];
            case SceneDeviceTypeThermostat:
                return _titleThermostat[row];
            case SceneDeviceTypeSwitch:
            case SceneDeviceTypeMotor:
            case SceneDeviceTypeDoorLock:
                return _titleFuncionOnOff[row];
            default:
                return @"";
        }
        
    }else {
        return _titleCondition[row];
    }
}

#pragma mark - ios UIAlertController
- (void)alertWithPicer:(UIPickerView *)picker
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
                                                                   message:@"\n\n\n\n\n\n\n\n\n\n\n"// change UIAlertController height
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    
    //Make a frame for the picker & then create the picker
    CGRect pickerFrame = CGRectMake(12, 12, 270, 250);
    picker.frame = pickerFrame;
    ios8Picker = picker;
    NSLog(@"%ld",(long)ios8Picker.tag);
    
    
    //set the pickers selection indicator to true so that the user will now which one that they are chosing
    [picker setShowsSelectionIndicator:YES];
    
    //Add the picker to the alert controller
    [alert.view addSubview:picker];
    
    //make the toolbar view
    UIToolbar *toolView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, alert.view.frame.size.width-16, kToolBarHeight)];
    toolView.backgroundColor = [UIColor blackColor]; //set it's background
    toolView.barStyle = UIBarStyleBlackTranslucent;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, kToolBarHeight)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    //    titleLabel.text = title;
    UIBarButtonItem *bbtTitle = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
    UIBarButtonItem *bbtSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *bbtOK = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(OKWithPicker)];
    bbtOK.width = 60.f;
    UIBarButtonItem *bbtCancel = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(CancleWithPicker)];
    bbtCancel.width = 60.f;
    toolView.items = [NSArray arrayWithObjects:bbtCancel,bbtSpace,bbtTitle,bbtSpace,bbtOK, nil];
    
    [alert.view addSubview:toolView];
    
    [self presentViewController:alert animated:NO completion:nil];
}

- (void)OKWithPicker
{
    NSInteger picker_selected_index = -1;
    if (ios8Picker.tag == PICKER_VIEW_DEVICE_LIST_TAG)
    {
        picker_selected_index= [_pickerView_DeviceList selectedRowInComponent:0];
        [self pickerViewConfirmForDevList:picker_selected_index];
        
    }else if (ios8Picker.tag == PICKER_VIEW_FUNCTION_LIST_TAG) {
        
        picker_selected_index= [_pickerView_FuncTitleList selectedRowInComponent:0];
        [self pickerViewConfirmForFunctionList:picker_selected_index];
        
    }else if (ios8Picker.tag == PICKER_VIEW_TAG_CONDITION) {
        
        picker_selected_index= [_pickerView_Condition selectedRowInComponent:0];
        [self pickerViewConfirmForCondition:picker_selected_index];
    }
    
    [self dismissModalViewControllerAnimated:YES];
}

- (void)CancleWithPicker
{
    [self dismissModalViewControllerAnimated:YES];
}

#pragma mark - UITableView Delegate
-(CGFloat)rowHeight
{
    return 76;
}

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row=[indexPath row];
    static NSString *ArmCellIdentifier = @"ArmCellIdentifier";
    ArmListViewCell *cell = (ArmListViewCell*)[dataTableView dequeueReusableCellWithIdentifier:ArmCellIdentifier];
    if (cell==nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ArmListViewCell" owner:self options:nil]lastObject];
        cell.delegate = self;
        cell.icon.image = [UIImage imageNamed:[icons objectAtIndex:indexPath.row%[icons count]]];
    }
    ArmModel *rowData = self.rows[row];
    cell.armData = rowData;
    
    return cell;
}

-(void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ArmItemInfoView  *view = [[[NSBundle mainBundle] loadNibNamed:@"ArmItemInfoView" owner:self options:nil]lastObject];
    view.delegate = self;
    [view initView];
    [view setArmItemInfoData:self.rows[indexPath.row]];
    [view pageStart];

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ArmModel *rowData = self.rows[indexPath.row];
    [self deleteArm:rowData];
}

#pragma mark - UITableViewCell Delegate
- (void)armListViewCellEditBtnWithArmData:(ArmModel *)data {
    
    ArmEditViewController *vc = [[ArmEditViewController alloc] initWithNibName:@"ArmEditViewController" bundle:nil];
    [vc setSelectDeviceList:self.deviceList];
    vc.armData = data;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)armListViewCellSwitch:(UISwitch *)sw valueChanged:(BOOL)isSwitchOn withArmData:(ArmModel *)data {
    
    [self requestArm:data toActive:isSwitchOn withSwitchCtl:sw];
}

#pragma mark -
- (void)armItemInfoView:(ArmItemInfoView *)view armChangeName:(NSString *)newName {
    
    
}

- (void)armItemInfoView:(ArmItemInfoView *)view armEnableNotify:(BOOL)isEnableNotify {
    
}

#pragma mark - SceneCell Delegate
- (void)changeInfo:(NSDictionary *)data cell:(SceneArmBaseTableViewCell *)cell {
    
    ChangeInfoDialog  *yijianview = [[[NSBundle mainBundle] loadNibNamed:@"ChangeInfoDialog" owner:self options:nil]lastObject];
    yijianview.delegate = self;
    [yijianview initView];
    [yijianview setSceneArmDData:data cell:cell];
    [yijianview pageStart];
}

@end
